<html>
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="../css/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/vue.min.js"></script>
    <script src="../js/axios.min.js"></script>
    <script src="../js/index.js"></script>
</head>
<body style="text-align: center;)">
<el-container>
    <div id="app">
        <h3>管理员登录</h3>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
            <el-form-item prop="name">
                <el-input type="text" prefix-icon="el-icon-user" v-model="ruleForm.name" autocomplete="off" placeholder="请输入账号"></el-input>
            </el-form-item>
            <el-form-item prop="pwd">
                <el-input type="password" prefix-icon="el-icon-lock" v-model="ruleForm.pwd" autocomplete="off" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button style="width: 100%;" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
        <div>其他登录方式：<a href="/dgutOauth"><img src="http://localhost:8080/img/dgut.png" width="20" height="20" style="position: relative;top: 5px"></a></div>
    </div>
</el-container>
</body>
<script>
    new Vue({
        el: "#app",
        data() {
            let validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入账号'));
                } else {
                    callback();
                }
            };
            let validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    mid: '',
                    name: '',
                    pwd: '',
                    username: ''
                },
                rules: {
                    name: [
                        { validator: validateName, trigger: 'blur' }
                    ],
                    pwd: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                let here = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let form = encodeURIComponent(JSON.stringify(this.ruleForm));
                        axios({
                            method: 'post',
                            url: '/dao/update/login',
                            data: form
                        }).then(function(response) {
                            let data = JSON.parse(decodeURIComponent(response.data));
                            if(data.success === "1") {
                                here.$message({
                                    message: '恭喜你，登录成功！',
                                    type: 'success'
                                });
                                window.setTimeout("window.location='http://localhost:8080/index.html'",1000);
                            } else {
                                here.$message.error('账号或密码错误！');
                            }
                        }).catch(function (error) {
                            if (error.response) {
                                here.$message.error('服务器正在开小差');
                            } else {
                                console.log('Error', error.message);
                            }
                            console.log(error.config);
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    });
</script>
</html>
